package com.sky.controller.subAccount;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.SubAccountLoginDTO;
import com.sky.entity.Employee;
import com.sky.entity.SubAccounts;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.SubAccountsService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import com.sky.vo.SubAccountsLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : RemSynch
 * @Date: 2024/11/1 上午10:12
 * @Description:
 */
@RestController
@RequestMapping("/public")
@Slf4j
@Api(tags = "公共接口")
public class publicManipulater {

    @Autowired
    private JwtProperties jwtProperties;

    @Resource
    private SubAccountsService accountsService;

    @PostMapping("/login")
    @ApiOperation(value = "镜像账号登录")
    public Result login(SubAccountLoginDTO accountLoginDTO) {
        log.info("员工登录：{}", accountLoginDTO);

        SubAccounts accounts = accountsService.login(accountLoginDTO);

        // 登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.ACCOUNT_ID, accounts.getId());
        claims.put("auth", "subAccounts");
        String token = JwtUtil.createJWT(
                jwtProperties.getSubAccountsSecretKey(),
                jwtProperties.getSubAccountsTtl(),
                claims);

        SubAccountsLoginVO employeeLoginVO = SubAccountsLoginVO.builder()
                .id(accounts.getId())
                .userName(accounts.getUsername())
                .name(accounts.getUsername())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }


}
